package com.example.composemodifiterdemo

import android.content.Intent
import android.os.Bundle
import android.util.Log
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.text.ClickableText
import androidx.compose.foundation.text.selection.SelectionContainer
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.text.*
import androidx.compose.ui.text.font.FontFamily
import androidx.compose.ui.text.font.FontStyle
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.style.TextDecoration
import androidx.compose.ui.text.style.TextOverflow
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp

/**
 * @auth: njb
 * @date: 2022/11/27 23:25
 * @desc: compose基础组件使用
 */
class BaseWeightActivity : ComponentActivity() {
    private val TAG = BaseWeightActivity::class.java.name;

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContent {
            //text文本控件
            TextDemo()
        }
    }

    @Preview
    @Composable
    fun TextDemo() {
        Column {
            //1.TextStyle：字体样式
            Spacer(modifier = Modifier.width(10.dp))
            Text(text = "Hello World \n" + "Goobye World")
            Spacer(modifier = Modifier.width(20.dp))
            Text(
                text = "Hello World \n" + "Goobye World",
                style = TextStyle(
                    fontSize = 25.sp,//字体大小
                    fontWeight = FontWeight.Bold,//字体粗细
                    background = Color.Cyan,//背景
                    lineHeight = 35.sp//行高
                ),
            )
            Spacer(modifier = Modifier.width(20.dp))
            Text(
                text = "Hello World",
                style = TextStyle(
                    color = Color.Gray,
                    letterSpacing = 4.sp)//字体间距
            )
            Spacer(modifier = Modifier.width(20.dp))
            Text(
                text = "Hello World",
                style = TextStyle(
                    textDecoration = TextDecoration.LineThrough,//删除线
                    fontSize = 10.sp,
                    color = Color.DarkGray
                )
            )
            Spacer(modifier = Modifier.width(20.dp))
            Text(
                text = "Hello World",
                style = MaterialTheme.typography.headlineLarge.copy(fontStyle = FontStyle.Italic),
                color = Color.Cyan,
                fontSize = 12.sp,
            )
            Spacer(modifier = Modifier.width(20.dp))
            Text(
                text = "你好世界，我正在使用Jetpack Compose框架来开发我的App界面",
                style = MaterialTheme.typography.bodyLarge.copy(fontFamily = FontFamily.Cursive),//字体风格
                fontSize = 16.sp,
                color = Color.Magenta
            )
            //2.MaxLines：行数
            Spacer(modifier = Modifier.width(20.dp))
            Text(
                text = "你好世界，我正在使用Jetpack Compose框架来开发我的App界面",
                style = MaterialTheme.typography.titleLarge.copy(fontStyle = FontStyle.Italic),
                fontSize = 16.sp,
                maxLines = 1,
                color = Color.Blue
            )
            Spacer(modifier = Modifier.width(20.dp))
            Text(
                text = "你好世界，我正在使用Jetpack Compose框架来开发我的App界面你好世界，我正在使用Jetpack Compose框架来开发我的App界面,你好世界，我正在使用Jetpack Compose框架来开发我的App界面",
                style = MaterialTheme.typography.labelLarge.copy(fontStyle = FontStyle.Normal),
                fontSize = 16.sp,
                maxLines = 1,
                overflow = TextOverflow.Ellipsis,
                color = Color.Red
            )
            //3.fontFamily：字体风格
            Spacer(modifier = Modifier.width(20.dp))
            Text(
                text = "你好世界，我正在使用Jetpack Compose框架来开发我的App界面你好世界，我正在使用Jetpack Compose框架来开发我的App界面,你好世界，我正在使用Jetpack Compose框架来开发我的App界面",
                style = MaterialTheme.typography.labelLarge.copy(fontFamily = FontFamily.Serif),
                fontSize = 16.sp,
                maxLines = 1,
                overflow = TextOverflow.Ellipsis,
                color = Color.Red
            )
            Spacer(modifier = Modifier.width(20.dp))
            Text(
                text = "你好世界，我正在使用Jetpack Compose框架来开发我的App界面你好世界，我正在使用Jetpack Compose框架来开发我的App界面,你好世界，我正在使用Jetpack Compose框架来开发我的App界面",
                style = MaterialTheme.typography.labelLarge.copy(fontFamily = FontFamily.SansSerif),
                fontSize = 16.sp,
                maxLines = 1,
                overflow = TextOverflow.Ellipsis,
                color = Color.Red
            )
            //4.AnnotatedString多样式文字
            Spacer(modifier = Modifier.width(20.dp))
            Text(
                text = buildAnnotatedString {
                    withStyle( style = SpanStyle(
                        fontSize = 16.sp)){
                        append("你现在学习的章节是")
                    }
                    withStyle (
                        style = SpanStyle(
                            fontWeight = FontWeight.W900,
                            fontSize = 16.sp
                        )
                    ){
                        append("Text")
                    }
                    append("\n")
                    withStyle (
                        style = ParagraphStyle(
                            lineHeight =16.sp,
                        )
                    ){
                        append("在刚刚讲过的内容中，我们学会了如何应用文字，以及如何限制文本的行数和处理溢出的视觉效果。")
                    }
                    append("\n")
                    append("现在，我们正在学习")
                    withStyle (
                        style = SpanStyle(
                            fontWeight = FontWeight.W900,
                            textDecoration = TextDecoration.Underline,
                            color = Color(0xFF59A869)
                        )
                    ){
                        append("AnnotatedString")
                    }
                }
            )
            Spacer(modifier = Modifier.width(20.dp))
            //clickText
            val annotatedText = buildAnnotatedString {
                // 在文本 “我不是罗大锤” 前后添加标签
                pushStringAnnotation(
                    tag = "URL",
                    annotation = "https://developer.android.google.cn/jetpack/compose/text"
                )
                withStyle(style = SpanStyle(
                    fontWeight = FontWeight.Bold,
                    textDecoration = TextDecoration.Underline,
                    color = Color.Blue
                )) {
                    append("AnnotatedString")
                }
                // 添加标签
                pop()
            }

            ClickableText(
                text = annotatedText,
                onClick = { offset ->
                    // 获取被点击区域 tag 为 URL 的 annotation
                    annotatedText.getStringAnnotations(
                        tag = "URL",
                        start = offset,
                        end = offset
                    ).firstOrNull()?.let { annotation ->
                        // 打开 URL
                        Log.d(TAG,"打开url" + annotation.item)
                        //openURL(annotation.item)
                       // intent = Intent(this@BaseWeightActivity,MoreTextActivity::class.java)
                        //intent = Intent(this@BaseWeightActivity,TextFixedSampleActivity::class.java)
                        //intent = Intent(this@BaseWeightActivity,ImageSampleActivity::class.java)
                       // intent = Intent(this@BaseWeightActivity,CustomGraphicalActivity::class.java)
                        //intent = Intent(this@BaseWeightActivity,AnimationActivity::class.java)
                       // intent = Intent(this@BaseWeightActivity,GesturesActivity::class.java)
                        //intent = Intent(this@BaseWeightActivity, InteractionSourceActivity::class.java)
                        //intent = Intent(this@BaseWeightActivity, DrawableActivity::class.java)
                        //intent = Intent(this@BaseWeightActivity, BrushActivity::class.java)
                       // intent = Intent(this@BaseWeightActivity,ButtonSampleActivity::class.java)
                       // intent = Intent(this@BaseWeightActivity,DialogSampleActivity::class.java)
                        //intent = Intent(this@BaseWeightActivity,CircleProgressActivity::class.java)
                       // intent = Intent(this@BaseWeightActivity, ComposeBaseWeightActivity::class.java)
                        //intent = Intent(this@BaseWeightActivity, CustomWeightActivity::class.java)
                       // intent = Intent(this@BaseWeightActivity, AlignmentLineActivity::class.java)
                        //intent = Intent(this@BaseWeightActivity, ConstraintLayoutActivity::class.java)
                       // intent = Intent(this@BaseWeightActivity,LinearLayoutActivity::class.java)
                       // intent = Intent(this@BaseWeightActivity,SurfaceActivity::class.java)
                        //intent = Intent(this@BaseWeightActivity,SpacerSampleActivity::class.java)
                        //intent = Intent(this@BaseWeightActivity,ScaffoldSampleActivity::class.java)
                        //intent = Intent(this@BaseWeightActivity,ListColumnSampleActivity::class.java)
                        //intent = Intent(this@BaseWeightActivity,ScaffoldSampleActivity::class.java)

                        intent = Intent(this@BaseWeightActivity,ColiSampleActivity::class.java)
                        startActivity(intent)
                    }
                }
            )
            SelectionContainer {
                Text("我是可被复制的")
            }
        }
    }
}